extends Control

@onready var start_button = $MenuContainer/MainMenu/StartButton
@onready var settings_button = $MenuContainer/MainMenu/SettingsButton
@onready var language_button = $MenuContainer/MainMenu/LanguageButton
@onready var quit_button = $MenuContainer/MainMenu/QuitButton
@onready var main_menu = $MenuContainer/MainMenu
@onready var settings_menu = $MenuContainer/SettingsMenu
@onready var language_menu = $MenuContainer/LanguageMenu
@onready var title = $Title

func _ready():
	# 连接按钮信号
	start_button.pressed.connect(_on_start_button_pressed)
	settings_button.pressed.connect(_on_settings_button_pressed)
	language_button.pressed.connect(_on_language_button_pressed)
	quit_button.pressed.connect(_on_quit_button_pressed)
	
	# 连接语言菜单按钮信号
	$MenuContainer/LanguageMenu/ChineseButton.pressed.connect(func(): _on_language_selected("zh"))
	$MenuContainer/LanguageMenu/EnglishButton.pressed.connect(func(): _on_language_selected("en"))
	$MenuContainer/LanguageMenu/BackButton.pressed.connect(_on_back_button_pressed)
	
	# 连接语言变更信号
	LanguageManager.language_changed.connect(_on_language_changed)
	
	# 初始化动画
	_setup_animations()
	
	# 初始显示主菜单，隐藏其他菜单
	show_menu("main")
	# 更新文本
	_update_text()

func _setup_animations():
	# 标题动画
	var title_tween = create_tween().set_loops()
	title_tween.tween_property(title, "modulate:a", 0.7, 1.0)
	title_tween.tween_property(title, "modulate:a", 1.0, 1.0)
	
	# 按钮进入动画
	var buttons = [start_button, settings_button, language_button, quit_button]
	for i in buttons.size():
		var button = buttons[i]
		button.modulate.a = 0
		button.position.x = -50
		
		var tween = create_tween()
		tween.tween_property(button, "position:x", 0, 0.3).set_delay(i * 0.1)
		tween.parallel().tween_property(button, "modulate:a", 1, 0.3)

func _on_button_hover(button: Button):
	var tween = create_tween()
	tween.tween_property(button, "scale", Vector2(1.1, 1.1), 0.1)

func _on_button_unhover(button: Button):
	var tween = create_tween()
	tween.tween_property(button, "scale", Vector2(1, 1), 0.1)

func _on_start_button_pressed():
	# 切换到游戏主场景
	get_tree().change_scene_to_file("res://scenes/main.tscn")

func _on_settings_button_pressed():
	show_menu("settings")

func _on_language_button_pressed():
	show_menu("language")

func _on_quit_button_pressed():
	get_tree().quit()

func _on_language_changed(_language: String):
	_update_text()

func _on_language_selected(language: String):
	LanguageManager.set_language(language)
	show_menu("main")

func _on_back_button_pressed():
	show_menu("main")

func _update_text():
	title.text = LanguageManager.get_text("game_title")
	start_button.text = LanguageManager.get_text("start_game")
	settings_button.text = LanguageManager.get_text("settings")
	language_button.text = LanguageManager.get_text("language")
	quit_button.text = LanguageManager.get_text("quit")
	$MenuContainer/LanguageMenu/BackButton.text = LanguageManager.get_text("back")
	$MenuContainer/SettingsMenu/BackButton.text = LanguageManager.get_text("back")

func show_menu(menu_name: String):
	var from_menu
	if main_menu.visible:
		from_menu = main_menu
	elif settings_menu.visible:
		from_menu = settings_menu
	else:
		from_menu = language_menu
	
	var to_menu
	
	# 隐藏所有菜单
	main_menu.visible = false
	settings_menu.visible = false
	language_menu.visible = false
	
	# 确定目标菜单
	match menu_name:
		"main":
			to_menu = main_menu
		"settings":
			to_menu = settings_menu
		"language":
			to_menu = language_menu
	
	# 显示目标菜单并播放动画
	to_menu.visible = true
	to_menu.modulate.a = 0
	to_menu.position.x = 100 if from_menu == main_menu else -100
	
	var tween = create_tween()
	tween.tween_property(to_menu, "position:x", 0, 0.3)
	tween.parallel().tween_property(to_menu, "modulate:a", 1, 0.3)
